package com.wujunshen.dumpling.common.utils;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

/**
 * JDBC工具
 *
 * @author frankwoo
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class JdbcUtils {

  /**
   * 释放资源
   *
   * @param rs {@link ResultSet}
   */
  public static void close(ResultSet rs) {
    if (!Objects.isNull(rs)) {
      try {
        rs.close();
      } catch (SQLException e) {
        log.error("exception message is:{}", ExceptionUtils.getStackTrace(e));
      }
    }
  }

  /**
   * 释放资源
   *
   * @param rs {@link ResultSet}
   * @param conn {@link Connection}
   */
  @SuppressWarnings("DuplicatedCode")
  public static void close(ResultSet rs, Connection conn) {
    if (!Objects.isNull(rs)) {
      try {
        rs.close();
      } catch (SQLException e) {
        log.error("exception message is:{}", ExceptionUtils.getStackTrace(e));
      }
    }
    if (!Objects.isNull(conn)) {
      try {
        conn.close();
      } catch (SQLException e) {
        log.error("exception message is:{}", ExceptionUtils.getStackTrace(e));
      }
    }
  }
}
